package com.qtz.demo.controller;

import com.qtz.demo.bean.Order;
import com.qtz.demo.bean.Question;
import com.qtz.demo.service.OrderServiceImpl;
import com.qtz.demo.service.QuestionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderServiceImpl orderService;
    @Autowired
    private QuestionServiceImpl questionService;

    //显示所有订单
    @RequestMapping(value = "/showAllOrders",method = RequestMethod.GET)
    @ResponseBody
    public List<Order> showAllOrders(){
        return orderService.showAllOrders();
    }

    //显示当前用户订单
    @RequestMapping(value = "/showByUid/{uid}",method = RequestMethod.GET)
    @ResponseBody
    public List<Order> showByUid(@PathVariable("uid") String uid){
        return orderService.showByUid(uid);
    }


    //添加订单
    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    @ResponseBody
    public boolean addOrder(@RequestBody Map<String,String> map){

        String current = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
        Timestamp time = Timestamp.valueOf(current);

        Order order = new Order();

        order.setUid(map.get("uid"));
        order.setTheme(map.get("theme"));
        order.setCreatetime(time);

        orderService.addOrder(order);

        String createtime = time.toString();

        Order list = orderService.findByTime(createtime);


        String oid = list.getOid();

        Question question = new Question();

        question.setOid(oid);
        question.setUid(map.get("uid"));
        question.setQuestion(map.get("question"));
        System.out.println(question.getQuestion());
        question.setCreatetime(time);

        return questionService.addQuestion(question);

    }


    //显示用户未完成订单
    @RequestMapping(value = "/showMyMakingOrder/{uid}",method = RequestMethod.GET)
    @ResponseBody
    public List<Order> showMyMakingOrder(@PathVariable("uid") String uid){
        return orderService.showMyMakingOrder(uid);
    }

    //修改订单状态
    @RequestMapping(value = "/finishOrder/{oid}",method = RequestMethod.GET)
    @ResponseBody
    public boolean finishOrder(@PathVariable("oid") String oid){
        System.out.println(oid);
        return orderService.finishOrder(oid);
    }

    //显示全部未完成订单
    @RequestMapping(value = "/showUndoOrder",method = RequestMethod.GET)
    @ResponseBody
    public List<Order> showMyMakingOrder(){
        return orderService.showUndoOrder();
    }

}
